<template>
  <div>
    <div class="page"></div>
  </div>
</template>
<script>
  import Pay from '@/dll/pay'
  import Util from '@/utils/util'

  export default {
    name: 'payIng',
    data () {
      return {
        wxPayUrl: '',
        // 订单编号
        orderSn: '',
        // 页面来源，order_confirm：确认订单，order_list:订单列表
        pageFrom: 'order_confirm',
      }
    },
    created () {
      this.pageFrom = this.$route.params.pageFrom || ''
      this.orderSn = this.$route.params.orderSn || ''
      if (this.orderSn) {
        this.createWxOrder()
      } else {
        this.$vux.alert.show({
          title: '提示',
          content: '支付过期，请重新支付。',
          onHide: () => {
            this.back()
          }
        })
      }
    },
    methods: {
      back () {
        if (this.pageFrom == 'order_confirm') {
          this.$router.back()
        } else {
          this.$router.replace({
            name: 'mallOrder',
            params: { tabIndex: 1 }
          })
        }
      },
      // 创建微信支付订单
      async createWxOrder () {
        let orderSn = this.orderSn
        let isWeixn = Util.isWeixn()
        let res = await Pay.createWxOrder(orderSn)
        if (res.code == 0) {
          if (isWeixn) {
            this.wxPayData = res.data
            if (typeof WeixinJSBridge == 'undefined') {
              if (document.addEventListener) {
                document.addEventListener(
                  'WeixinJSBridgeReady',
                  onBridgeReady,
                  false
                )
              } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady)
                document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady)
              }
            } else {
              this.onBridgeReady()
            }
          } else {
            this.$router.replace({
              name: 'pay',
              query: {
                pageFrom: this.pageFrom
              },
              params: {
                orderSn: this.orderSn,
                tradeNo: res.data.tradeNo,
                wxPayUrl: res.data.mweb_url
              }
            })
          }
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: res.msg,
            onHide: () => {
              this.back()
            }
          })
        }
      },
      onBridgeReady (data) {
        let wxPayData = {
          appId: this.wxPayData.appId,
          timeStamp: this.wxPayData.timestamp,
          nonceStr: this.wxPayData.nonceStr,
          package: this.wxPayData.package,
          signType: this.wxPayData.signType,
          paySign: this.wxPayData.paySign
        }
        WeixinJSBridge.invoke('getBrandWCPayRequest', wxPayData, res => {
          let msg = '未知错误'
          if (res.err_msg == 'get_brand_wcpay_request:ok') {
            this.$router.replace({
              name: 'pay',
              query: {
                pageFrom: this.pageFrom
              },
              params: { tradeNo: this.wxPayData.tradeNo, orderSn: this.orderSn }
            })
            return
          }
          if (res.err_msg == 'get_brand_wcpay_request:fail') {
            msg = '支付失败'
          }
          if (res.err_msg == 'get_brand_wcpay_request:cancel') {
            msg = '用户取消支付'
          }
          this.$vux.alert.show({
            title: '提示',
            content: msg,
            onHide: () => {
              this.back()
            }
          })
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  .wxPayUrl {
    margin: 0;
    padding: 0;
    height: 100vh;
    width: 100vw;
    border: none;
  }
</style>
